package org.apache.lucene.demo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * 保存group的值
 * @author fxcai
 *
 */
public class GroupValue {
	
	/**
	 * 保存group的中间值
	 * Map<卡口编号,plateMap>
	 */
	private Map<String,Map<String,Long>> bayonetMap=new HashMap<String, Map<String,Long>>();

	
	/**
	 * 保存group的值
	 * 线程安全Map<卡口编号,plateMap>，用来合并各个线程的结果集
	 */
	private ConcurrentHashMap<String, ConcurrentHashMap<String, Long>> rootMap=new ConcurrentHashMap<String, ConcurrentHashMap<String,Long>>();
	
	
	
	/**
	 * 将各个线程的结果集bayonetMap放入rootMap中
	 * @param bayonetMap
	 */
	public void putRootmap(Map<String,Map<String,Long>> bayonetMap){
		for(String kkbh:bayonetMap.keySet()){
			if(rootMap.containsKey(kkbh)){
				Map<String, Long> rootPlateMap=rootMap.get(kkbh);
				Map<String, Long> plateMap=bayonetMap.get(kkbh);
				for(String plate:plateMap.keySet()){
					if(rootPlateMap.containsKey(plate)){
						rootPlateMap.put(plate, rootPlateMap.get(plate)+plateMap.get(plate));
					}else{
						rootPlateMap.put(plate, plateMap.get(plate));
					}
				}
			}else{
				ConcurrentHashMap<String, Long> rootPlateMap=new ConcurrentHashMap<String, Long>();
				Map<String, Long> plateMap=bayonetMap.get(kkbh);
				for(String plate:plateMap.keySet()){
					rootPlateMap.put(plate, plateMap.get(plate));
				}
				rootMap.put(kkbh, rootPlateMap);
			}
			
		}
	}
	
	
    /**
     * 将卡口编号，车牌号码，放入bayonetMap中
     * @param kkbh
     * @param cthphm
     */
	public void putBayonetMap(String kkbh,String cthphm){
		if(bayonetMap.containsKey(kkbh)){
			this.putPlateMap(cthphm, bayonetMap.get(kkbh));
		}else{
			Map<String, Long> tempMap=new HashMap<String, Long>();
			tempMap.put(cthphm, 1l);
			bayonetMap.put(kkbh, tempMap);
		}
	}
	
	
	/**
	 * 将车牌号码，频度放入plateMap中
	 * @param cthphm
	 */
	private void putPlateMap(String cthphm,Map<String, Long> plateMap){
		if(plateMap.containsKey(cthphm)){
			plateMap.put(cthphm, plateMap.get(cthphm)+1);
		}else{
			plateMap.put(cthphm, 1l);
		}
	}
	
	
	public ConcurrentHashMap<String, ConcurrentHashMap<String, Long>> getRootMap() {
		return rootMap;
	}
	public void setRootMap(
			ConcurrentHashMap<String, ConcurrentHashMap<String, Long>> rootMap) {
		this.rootMap = rootMap;
	}
	public Map<String, Map<String, Long>> getBayonetMap() {
		return bayonetMap;
	}
	public void setBayonetMap(Map<String, Map<String, Long>> bayonetMap) {
		this.bayonetMap = bayonetMap;
	}
}
